package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ioftv.entity.BusinessStatistics;
import com.ioftv.mapper.BusinessStatisticsMapper;
import com.ioftv.service.BusinessStatisticsService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 业务统计Service实现类
 */
@Service
public class BusinessStatisticsServiceImpl extends BaseService<BusinessStatisticsMapper, BusinessStatistics> implements BusinessStatisticsService {

    @Override
    public BusinessStatistics getByStatDate(LocalDate statDate) {
        LambdaQueryWrapper<BusinessStatistics> queryWrapper = createUserQueryWrapper();
        queryWrapper.eq(BusinessStatistics::getStatDate, statDate);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<BusinessStatistics> getByDateRange(LocalDate startDate, LocalDate endDate) {
        LambdaQueryWrapper<BusinessStatistics> queryWrapper = createUserQueryWrapper();
        queryWrapper.ge(BusinessStatistics::getStatDate, startDate);
        queryWrapper.le(BusinessStatistics::getStatDate, endDate);
        queryWrapper.orderByDesc(BusinessStatistics::getStatDate);
        return this.list(queryWrapper);
    }

    @Override
    public BusinessStatistics getByStatDateAndUserId(LocalDate statDate, Long userId) {
        LambdaQueryWrapper<BusinessStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusinessStatistics::getStatDate, statDate);
        queryWrapper.eq(BusinessStatistics::getUserId, userId);
        return this.getOne(queryWrapper);
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<BusinessStatistics> queryWrapper) {
        queryWrapper.eq(BusinessStatistics::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(BusinessStatistics entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}